<?php
class Kbs_Application_Resource_Authhttp extends Zend_Application_Resource_ResourceAbstract
{
    /**
     * Auth http
     *
     */
    public function init()
    {
        $auth = Zend_Auth::getInstance();

        //$namespace = self::initConfig('Session')->namespace->auth->http;
        //require_once('Extension/Auth/Storage/Session.php');
        //$storage = new Extension_Auth_Storage_Session($namespace);
        //$auth->setStorage($storage);

        if (!$auth->hasIdentity()) {

            $options = $this->getOptions();

            $basicResolver = new Zend_Auth_Adapter_Http_Resolver_File();
            $basicResolver->setFile(PROJECT_ROOT . $options['resolver']['basic']);
            $digestResolver = new Zend_Auth_Adapter_Http_Resolver_File();
            $digestResolver->setFile( PROJECT_ROOT . $options['resolver']['digest']);

            $authAdapterHttp = new Zend_Auth_Adapter_Http($options['options']);

            $request = Zend_Controller_Front::getInstance()->getRequest();
            $responce = Zend_Controller_Front::getInstance()->getResponse();
            $authAdapterHttp->setRequest($request);
            $authAdapterHttp->setResponse($responce);

            $authAdapterHttp->setBasicResolver($basicResolver);
            $authAdapterHttp->setDigestResolver($digestResolver);

            //Zend_Registry::set('authAdapterHttp', $authAdapterHttp);

            $auth = Zend_Auth::getInstance();
            $result = $auth->authenticate($authAdapterHttp);

            if (!$result->isValid()) {
                //echo md5('admin') . '<br />';
                //echo md5('site') . '<br />';
                //echo md5('admin') . '<br />';
                //echo MD5('kimho:site:admin') . '<br />';
                //echo base64_encode('kim') . '<br />';
                //file_put_contents(PROJECT_ROOT . $config->resolver->digest, 'kim:site:'.md5('kim'));

                echo $authAdapterHttp->getResponse();
                echo $messages = implode('<br />', $result->getMessages());
                exit();
            }
        }
    }
}